from selenium import webdriver
from selenium.webdriver.common.by import By
from webdriver_manager.chrome import ChromeDriverManager
from time import sleep
from pandas import DataFrame
import pandas as pd
import re
import os


class DouBanSpider():
    def __init__(self):
        self.driver = webdriver.Chrome(ChromeDriverManager().install())
        self.url = "https://movie.douban.com/"

    def find_element(self):
        # name = input("请输入演员名字")
        name = "蔡徐坤"
        self.driver.find_element(By.ID, "inp-query").send_keys(name)
        sleep(1)
        self.driver.find_element(By.CLASS_NAME, "inp-btn").click()

    # 获取数据
    def parse_content(self):
        items = self.driver.find_elements(By.XPATH, '//div[@id="root"]//div[@class="item-root"]')
        d = {"title": [], "rating_nums": [], "pl": [], "meta_abstract": [], "meta_abstract_2": []}
        for item in items[1:]:
            title = item.find_element(By.CLASS_NAME, "title-text").text
            try:
                rating_nums = item.find_element(By.CLASS_NAME, "rating_nums").text
            except Exception as e:
                rating_nums = 0
            pl = item.find_element(By.CLASS_NAME, "pl").text
            if "评价" not in pl:
                pl = 0
            else:
                pl = re.search("\d+", pl).group(0)
            meta_abstract = item.find_element(By.XPATH, './/div[@class="meta abstract"]').text
            meta_abstract_2 = item.find_element(By.XPATH, './/div[@class="meta abstract_2"]').text
            d["title"].append(title)
            d["rating_nums"].append(rating_nums)
            d["pl"].append(pl)
            d["meta_abstract"].append(meta_abstract)
            d["meta_abstract_2"].append(meta_abstract_2)
        print(d)
        self.save_content(d)

        # 继续爬取一下
        self.parse_next_content()

    def parse_next_content(self):
        try:
            next = self.driver.find_element(By.XPATH, '//a[@class="next false"]')
            next.click()
            sleep(1)
            # 解析数据
            self.parse_content()
        except Exception as e:
            # 退出
            self.driver.quit()
            print(e)

    def save_content(self, data):
        if os.path.exists("movie.xlsx"):
            # 原数据
            df = pd.read_excel("movie.xlsx",sheet_name="Sheet1")
            # 新数据
            df1 = DataFrame(data)
            # 追加
            df = df.append(df1)
        else:
            df = DataFrame(data)
        df.to_excel("movie.xlsx", index=False, )

    def start(self):
        self.driver.get(self.url)
        self.driver.maximize_window()

        # 定位元素
        self.find_element()

        sleep(1)

        # 解析数据
        self.parse_content()


if __name__ == '__main__':
    dbs = DouBanSpider()
    dbs.start()
